<?php
	
	require_once($C->INCPATH.'conf_embed.php');
	//2011.3.1 add by vincent to enable comment include a picture
	require_once($C->INCPATH.'helpers/func_images.php');
	//end of - 2011.3.1 add by vincent to enable comment include a picture

	
	class newpostcomment
	{
		private $network	= FALSE;
		private $id		= FALSE;
		private $user	= FALSE;
		private $post	= FALSE;
		private $api_id		= 0;
		private $message		= '';
		private $mentioned	= array();
		private $posttags		= 0;
		private $db1;
		private $db2;
		private $cache;
		public $error	= FALSE;
		//2011.3.1 add by vincent to enable comment include a picture
		private $attached		= array();
		//end of - 2011.3.1 add by vincent to enable comment include a picture
		
		public function __construct($post_obj)
		{
			global $C;
			$this->cache	= & $GLOBALS['cache'];
			$this->db1		= & $GLOBALS['db1'];
			$this->db2		= FALSE;
			$this->post		= & $post_obj;
			if( ! $this->post instanceof post ) {
				$this->error	= TRUE;
				return;
			}
			$this->api_id	= $C->API_ID;
			$n	= & $GLOBALS['network'];
			if( $n->id ) {
				$u	= & $GLOBALS['user'];
				if($u->is_logged && $u->network->id==$n->id) {
					$this->network	= $n;
					$this->user		= $u;
					$this->db2	= & $GLOBALS['db2'];
				}
			}
			$this->id	= FALSE;
		}
		
		public function set_user($network_id, $user_id)
		{
			$this->network	= FALSE;
			$this->user		= FALSE;
			$n	= new network;
			if( ! $n->LOAD_by_id($network_id) ) {
				return FALSE;
			}
			if( ! $u = $n->get_user_by_id($user_id) ) {
				return FALSE;
			}
			$this->network	= $n;
			$this->user		= $u;
			return TRUE;
		}
		
		public function set_api_id($api_id)
		{
			if( $this->id ) {
				return FALSE;
			}
			$this->api_id	= intval($api_id);
			return TRUE;
		}
		
		public function set_message($message)
		{
			if( empty($message) ) {
				return FALSE;
			}
			global $C;
			$this->message	= trim($message);
			
			$this->mentioned	= array();
			if( preg_match_all('/\@([a-zA-Z0-9\-_]{3,30})/u', $message, $matches, PREG_PATTERN_ORDER) ) {
				foreach($matches[1] as $unm) {
					if( $usr = $this->network->get_user_by_username($unm) ) {
						$this->mentioned[]	= $usr->id;
					}
				}
			}
			$this->mentioned	= array_unique($this->mentioned);
			
			$this->posttags	= array();
			if( preg_match_all('/\#([א-תÀ-ÿ一-龥а-яa-z0-9ա-ֆ\-_]{1,50})/iu', $message, $matches, PREG_PATTERN_ORDER) ) {
				foreach($matches[1] as $tg) {
					$this->posttags[]	= mb_strtolower(trim($tg));
				}
			}
			$this->posttags	= count( array_unique($this->posttags) );
		}
		
		//2011.3.1 add by vincent, to enable the comment include a picture
		public function attach_image($input, $orig_filename='')
		{
			global $C;
			if( isset($this->attached['image']) ) {
				unset($this->attached['image']);
			}
			$types	= array (
				IMAGETYPE_GIF	=> 'gif',
				IMAGETYPE_JPEG	=> 'jpg',
				IMAGETYPE_PNG	=> 'png',
			);
			if( preg_match('/^(http|https|ftp)\:\/\//u', $input) ) {
				$tmp	= $C->TMP_DIR.'tmp'.md5(time().rand()).'.'.pathinfo($input,PATHINFO_EXTENSION);
				$res	= my_copy($input, $tmp);
				if( ! $res ) {
					return FALSE;
				}
				chmod($tmp, 0666);
				$input	= $tmp;
			}
			list($w, $h, $tp)	= @getimagesize($input);
			if( $w == 0 || $h == 0 ) {
				return FALSE;
			}
			if( ! isset($types[$tp]) ) {
				return FALSE;
			}
			$fn	= time().rand(100000,999999);
			$data	= (object) array (
				'in_tmpdir'	=> TRUE,
				'title'	=> $orig_filename,
				'file_original'	=> $fn.'_orig.'.$types[$tp],
				'file_preview'	=> $fn.'_large.'.$types[$tp],
				'file_thumbnail'	=> $fn.'_thumb.'.$types[$tp],
				'size_original'	=> '',
				'size_preview'	=> '',
				'filesize'	=> 0,
				'hits'	=> 0,
			);
			$data	= copy_attachment_image($input, $data); 
			if( ! $data ) {
				return FALSE;
			}
			rm($input);
			return $this->attached['image'] = $data;
		}
		
		public function get_imagefile_name(){
			if( empty($this->attached) || empty( $this->attached['image']) || empty( $this->attached['image']->file_original) ){
				return '';
			}else{
				return $this->attached['image']->file_original;
			}
		}
		
		private function attachments_copy_files()
		{
			global $C;
			$dir	= $C->IMG_DIR.'attachments/'.$this->network->id.'/';
			if( ! is_dir($dir) ) {
				mkdir($dir, 0777);
			}
			foreach($this->attached as &$at) {
				if( !isset($at->in_tmpdir) || !$at->in_tmpdir ) {
					continue;
				}
				foreach($at as $k=>$v) {
					if( substr($k,0,5) != 'file_' ) {
						continue;
					}
					if( empty($v) ) {
						continue;
					}
					rename($C->TMP_DIR.$v, $dir.$v);
					chmod($dir.$v, 0777);
				}
				unset($at->in_tmpdir);
			}
			return TRUE;
		}
		//end of - 2011.3.1 add by vincent, to enable the comment include a picture
		
		public function save()
		{
			if( $this->error ) {
				return FALSE;
			}
			if( ! $this->user->is_logged ) {
				return FALSE;
			}
			if( empty($this->message) ) {
				return FALSE;
			}
			$db2		= & $this->network->db2;
			$is_private	= $this->post->post_type=='private' ? TRUE : FALSE;
			$db_api_id		= intval($this->api_id);
			$db_user_id		= intval($this->user->id);
			$db_message		= $db2->escape($this->message);
			$db_mentioned	= count($this->mentioned);
			$db_posttags	= intval($this->posttags);
			$db_date		= time();
			$db_ip_addr		= ip2long($_SERVER['REMOTE_ADDR']);
			
			$db2->query('INSERT INTO '.($is_private?'posts_pr_comments':'posts_comments').' SET api_id="'.$db_api_id.'", post_id="'.$this->post->post_id.'", user_id="'.$db_user_id.'", message="'.$db_message.'", mentioned="'.$db_mentioned.'", posttags="'.$db_posttags.'", date="'.$db_date.'", ip_addr="'.$db_ip_addr.'"   ');
			if( ! $id = $db2->insert_id() ) {
				return FALSE;
			}
			$db2->query('UPDATE '.($is_private?'posts_pr':'posts').' SET comments=comments+1, date_lastcomment="'.time().'" WHERE id="'.$this->post->post_id.'" LIMIT 1');

			//2011.3.1 add by vincent, to enable the comment include a picture
			$this->attachments_copy_files();
			foreach($this->attached as $k=>$v) {
				$sql_comment_attach = 'INSERT INTO '.($is_private?'posts_comments_pr_attachments':'posts_comments_attachments').' SET post_id="'.$this->post->post_id.'",comment_id="'.$id.'", type="'.$db2->escape($k).'", data="'.$db2->escape(serialize($v)).'" ';
				$db2->query($sql_comment_attach);
			}
			//end of - 2011.3.1 add by vincent, to enable the comment include a picture
		
			foreach($this->mentioned as $uid) {
				$db2->query('INSERT INTO '.($is_private?'posts_pr_comments_mentioned':'posts_comments_mentioned').' SET comment_id="'.$id.'", user_id="'.intval($uid).'" ');
			}
			
			$db2->query('SELECT id, newcomments FROM '.($is_private?'posts_pr_comments_watch':'posts_comments_watch').' WHERE user_id="'.$this->user->id.'" AND post_id="'.$this->post->post_id.'" LIMIT 1');
			if( $sdf = $db2->fetch_object() ) {
				if( $sdf->newcomments <> 0 ) {
					$db2->query('UPDATE '.($is_private?'posts_pr_comments_watch':'posts_comments_watch').' SET newcomments=0 WHERE id="'.$sdf->id.'" LIMIT 1');
				}
			}
			else {
				$db2->query('INSERT INTO '.($is_private?'posts_pr_comments_watch':'posts_comments_watch').' SET user_id="'.$this->user->id.'", post_id="'.$this->post->post_id.'", newcomments=0');
			}
			if( ! $is_private ) {
				$db2->query('SELECT user_id FROM '.($is_private?'posts_pr_comments_watch':'posts_comments_watch').' WHERE user_id<>"'.$this->user->id.'" AND post_id="'.$this->post->post_id.'" ');
				while( $sdf = $db2->fetch_object() ) {
					$this->network->set_dashboard_tabstate($sdf->user_id, 'commented', 1);
				}
				//foreach($this->mentioned as $uid) {
				//	За тези хора трябва да се вкарва ред в posts_pr_comments_watch (ако постът е в достъпна за тях група)
				//	Също така да се известяват по e-mail за @user
				//}
			}
			$db2->query('UPDATE '.($is_private?'posts_pr_comments_watch':'posts_comments_watch').' SET newcomments=newcomments+1 WHERE user_id<>"'.$this->user->id.'" AND post_id="'.$this->post->post_id.'" ');
			return $id;
		}
	}
	
?>